<?php

require '../utils/conn.php';
include_once '../models/topic.php';
include_once '../models/set.php';
include 'getTopic.php';

function getSetById($sid) {
	$query = "SELECT * FROM t_set WHERE s_id = ".$sid;
	$result = mysql_query($query)  or die("查询单词出现错误：". mysql_error());;
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$set = new Set();
	$set->setSetId($row['s_id']);
	$set->setName($row['s_name']);
	$set->setTimestamp($row['s_timestamp']);
	$set->setListening(getTopicById($row['listening_id']));
	$set->setSpeaking(getTopicById($row['speaking_id']));
	$set->setReading(getTopicById($row['reading_id']));
	$set->setWriting(getTopicById($row['writing_id']));
	return $set;
}

function getSetBaseInfoById($sid) {
	$query = "SELECT * FROM t_set WHERE s_id = ".$sid;
	$result = mysql_query($query)  or die("查询单词出现错误：". mysql_error());;
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$set = new Set();
	$set->setSetId($row['s_id']);
	$set->setName($row['s_name']);
	$set->setTimestamp($row['s_timestamp']);
	$set->setListening(getTopicBaseInfoById($row['listening_id']));
	$set->setSpeaking(getTopicBaseInfoById($row['speaking_id']));
	$set->setReading(getTopicBaseInfoById($row['reading_id']));
	$set->setWriting(getTopicBaseInfoById($row['writing_id']));
	return $set;
}

function getAllSets() {
	$query = "SELECT * FROM t_set ORDER BY s_timestamp DESC";
	$sets = array();
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$set = new Set();
		$set->setSetId($row['s_id']);
		$set->setName($row['s_name']);
		$set->setTimestamp($row['s_timestamp']);
		if ($row['listening_id'] == -1) {
			$set->setListening(null);
		}
		else {
			$set->setListening(getTopicBaseInfoById($row['listening_id']));
		}
		if ($row['speaking_id'] == -1) {
			$set->setSpeaking(null);
		}
		else {
			$set->setSpeaking(getTopicBaseInfoById($row['speaking_id']));
		}
		if ($row['reading_id'] == -1) {
			$set->setReading(null);
		}
		else {
			$set->setReading(getTopicBaseInfoById($row['reading_id']));
		}
		if ($row['writing_id'] == -1) {
			$set->setWriting(null);
		}
		else {
			$set->setWriting(getTopicBaseInfoById($row['writing_id']));
		}
		$sets[] = $set;
	}
	return $sets;
}

?>